package org.example.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;


//图片验证码
@Controller
@RequestMapping("/captcha")
public class SysCaptchaController {
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;
    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @GetMapping("/captchaImage")
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream out = null;
        try{
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");

            String type = request.getParameter("type");

            String captchaStr;
            String code = null;
            BufferedImage captchaImage = null;

            if("char".equals(type)){
                //生成验证码文本
                captchaStr = code = captchaProducer.createText();
                //生成验证码图片
                captchaImage = captchaProducer.createImage(captchaStr);
            }else if("math".equals(type)){
                String captchaText = captchaProducerMath.createText();//6-0=?@6
                captchaStr = captchaText.substring(0,captchaText.lastIndexOf("@"));//6-0=?
                code = captchaText.substring(captchaText.lastIndexOf("@")+1);//6

                captchaImage = captchaProducerMath.createImage(captchaStr);
            }
            //生成验证码文本
            //String captchaText = captchaProducer.createText();
            //生成验证码图片
            //BufferedImage captchaImage = captchaProducer.createImage(captchaText);
            //session.setAttribute("captcha",captchaText);

            //将验证码文本存储在Session中
            HttpSession session= request.getSession();
            session.setAttribute(Constants.KAPTCHA_SESSION_KEY,code);
            //将验证码图片输出到响应
            out = response.getOutputStream();
            //该方法将验证码图片以jpg格式写入输入流
            ImageIO.write(captchaImage,"jpg",out);
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(out != null){
                    out.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

        return null;
    }

    @GetMapping("/verify")
    @ResponseBody
    public String verify(String verify,HttpSession session){
        System.out.println(verify);
        System.out.println(session.getAttribute(Constants.KAPTCHA_SESSION_KEY));
        String captcha = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        System.out.println(captcha);
        if(!verify.equals(captcha)){
            return "false";
        }else{
            return "true";
        }

    }

}
